package stack_queue;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;

/**
 * Created with IntelliJ IDEA.
 * Description: 队列
 * User: 王圆豪
 * Date: 2024-08-14
 * Time: 18:12
 */
public class MyQueue {
    static class ListNode{
        ListNode prev;
        ListNode next;
        int val;

        public ListNode(int val) {
            this.val = val;
        }
    }

    ListNode first;
    ListNode last;

    public void offer(int val) {
        ListNode cur = new ListNode(val);
        if (first == null) {
            last = cur;
            first = cur;
        }else {
            last.next = cur;
            cur.prev = last;
            last = last.next;
        }
    }

    public int poll() {
        if (isEmpty()) {
            return -1;
        }
        int k = first.val;
        first = first.next;
        if (first != null) {
            first.prev = null;
        }
        return k;
    }

    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return first.val;
    }

    public boolean isEmpty() {
        return first == null;
    }
}
